//
//  UIView+Tap.swift
//  Pods
//
//  Created by 100Years on 2025/7/24.
//

import UIKit
public extension UIView {
    private struct AssociatedKeys {
        static var tapAction = "tapAction"
    }
    
    typealias TapAction = () -> Void
    
    /// 添加点击手势（闭包版本）
    func addTapAction(_ action: @escaping TapAction) {
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTSSmalCoaCoPodsTap))
        tap.cancelsTouchesInView = false
        self.addGestureRecognizer(tap)
        self.isUserInteractionEnabled = true
        
        // 存储闭包
        objc_setAssociatedObject(self, &AssociatedKeys.tapAction, action, .OBJC_ASSOCIATION_COPY_NONATOMIC)
    }
    
    @objc private func handleTSSmalCoaCoPodsTap(_ gesture: UITapGestureRecognizer) {
        if let action = objc_getAssociatedObject(self, &AssociatedKeys.tapAction) as? TapAction {
            action()
        }
    }
}
